var t = require("../../../@babel/runtime/helpers/defineProperty"),
  e = f(require("../../../@babel/runtime/regenerator")),
  a = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  i = require("../../../utils/index"),
  n = f(require("../../../config/index")),
  o = (function (t) {
    if (t && t.__esModule) return t;
    if (null === t || ("object" != typeof t && "function" != typeof t))
      return { default: t };
    var e = l();
    if (e && e.has(t)) return e.get(t);
    var a = {},
      i = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var n in t)
      if (Object.prototype.hasOwnProperty.call(t, n)) {
        var o = i ? Object.getOwnPropertyDescriptor(t, n) : null;
        o && (o.get || o.set) ? Object.defineProperty(a, n, o) : (a[n] = t[n]);
      }
    (a.default = t), e && e.set(t, a);
    return a;
  })(require("@/services/base/api")),
  r = f(require("@/config/page")),
  u = require("@/utils/jump"),
  s = f(require("dayjs")),
  d = require("@/services/user-service"),
  c = require("@/utils/report");
function l() {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap();
  return (
    (l = function () {
      return t;
    }),
    t
  );
}
function f(t) {
  return t && t.__esModule ? t : { default: t };
}
Page({
  data: {
    title: "展览详情",
    isSubscribe: !1,
    subscribed: !1,
    defaultImg: n.default.defaultImg,
    position: !0,
    isShowLoginPop: !1,
    color: "#fff",
    titleColor: "#fff",
    background:
      "linear-gradient(0deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.5) 100%)",
    iconTheme: "white",
    CDN: n.default.CDN,
    current: 0,
    imgInfo: "https://industry.map.qq.com/cloud/sanxingdui/exhibition-card",
    info: {},
    list: [],
    more: !1,
    exhibition_id: "",
    audio_duration: 0,
    audio_current: "00:00",
    audio_status: 0,
    currentAudio: 0,
    lastAudio: 0,
    isShow: !1,
    duration: "0%",
    current_time: "00:00",
    audio_duration_val: "00:00",
    footerAudio: 0,
    isCollection: !1,
    newList: [],
    info_duration: "00:00",
    paused: !0,
    progress: 0,
    openId: "",
    times: 0,
    type: 2,
    longitude: 1,
    latitude: 1,
  },
  bindViewTap: function () {},
  onLoad: function (t) {
    var e = t.id,
      a = t.scene;
    this.setData({ exhibition_id: e || a });
  },
  onShow: function () {
    var t = this;
    return a(
      e.default.mark(function i() {
        var n;
        return e.default.wrap(function (i) {
          for (;;)
            switch ((i.prev = i.next)) {
              case 0:
                return (
                  wx.getBackgroundAudioManager().stop(),
                  t.setData({ isShow: !1 }),
                  (n = t),
                  (i.next = 5),
                  wx.getLocation({
                    type: "wgs84",
                    success: function (t) {
                      return a(
                        e.default.mark(function a() {
                          return e.default.wrap(function (e) {
                            for (;;)
                              switch ((e.prev = e.next)) {
                                case 0:
                                  n.setData({
                                    latitude: t.latitude,
                                    longitude: t.longitude,
                                  }),
                                    n.initData();
                                case 2:
                                case "end":
                                  return e.stop();
                              }
                          }, a);
                        }),
                      )();
                    },
                    fail: function () {
                      return a(
                        e.default.mark(function t() {
                          return e.default.wrap(function (t) {
                            for (;;)
                              switch ((t.prev = t.next)) {
                                case 0:
                                  n.initData();
                                case 1:
                                case "end":
                                  return t.stop();
                              }
                          }, t);
                        }),
                      )();
                    },
                  })
                );
              case 5:
              case "end":
                return i.stop();
            }
        }, i);
      }),
    )();
  },
  initData: function () {
    var n = this;
    return a(
      e.default.mark(function a() {
        var r,
          u,
          l,
          f,
          g,
          h,
          p,
          v,
          w,
          m,
          x,
          b,
          _,
          A,
          D,
          k,
          y,
          P,
          M,
          I,
          S,
          C,
          T,
          B,
          L,
          E,
          N;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                return (e.next = 2), (0, d.getUserId)();
              case 2:
                return (
                  (_ = e.sent),
                  (A = n.data),
                  (D = A.longitude),
                  (k = A.latitude),
                  (y = A.type),
                  (P = A.exhibition_id),
                  (e.next = 6),
                  o.myCollectionList({
                    type: y,
                    openId: _,
                    longitude: D,
                    latitude: k,
                    relative_ids: P,
                  })
                );
              case 6:
                return (
                  (M = e.sent),
                  (e.next = 9),
                  Promise.all([
                    o.exhibitionInfo({ exhibition_id: [P], open_id: _ }),
                    o.getCollectionList({ exhibition_id: P }),
                  ])
                );
              case 9:
                return (
                  (I = e.sent),
                  console.log(I, "res"),
                  (S = I[1].data.list),
                  n.setData({
                    openId: _,
                    isCollection: !!M.data.date.length,
                    info:
                      null !== (r = I[0]) &&
                      void 0 !== r &&
                      null !== (u = r.data) &&
                      void 0 !== u &&
                      u[0]
                        ? I[0].data[0]
                        : {},
                    list:
                      null !== (l = I[1]) &&
                      void 0 !== l &&
                      null !== (f = l.data) &&
                      void 0 !== f &&
                      null !== (g = f.list) &&
                      void 0 !== g &&
                      g.length
                        ? S.map(function (t) {
                            var e;
                            return (
                              null !== (e = t.audio) &&
                                void 0 !== e &&
                                e.length &&
                                (t.audio[0].time = (0, i.format)(
                                  t.audio[0].time,
                                )),
                              t
                            );
                          })
                        : [],
                  }),
                  console.log(n.data.info, "this.data.info"),
                  (C = []),
                  n.data.list.length &&
                    (C = n.data.list.map(function (t) {
                      return t.exhibit_id;
                    })),
                  (e.next = 18),
                  o.postExhibitInfo({ exhibit_id: C })
                );
              case 18:
                null != (T = e.sent) &&
                  null !== (h = T.data) &&
                  void 0 !== h &&
                  h.length &&
                  T.data.forEach(function (e) {
                    var a = n.data.list.findIndex(function (t) {
                      return t.exhibit_id === e.exhibit_id;
                    });
                    if (-1 !== a) {
                      var i = "list[".concat(a, "]");
                      n.setData(t({}, i, Object.assign(e, n.data.list[a])));
                    }
                  }),
                  (B = n.data),
                  (L = B.info),
                  (E = B.list),
                  (N = E.length
                    ? (0, i.deepClone)(I[1].data.list).filter(function (t) {
                        var e;
                        return null === (e = t.audio) || void 0 === e
                          ? void 0
                          : e[0];
                      })
                    : []),
                  null !== (p = L.audio) &&
                    void 0 !== p &&
                    p[0] &&
                    N.unshift(L),
                  n.setData({
                    subscribed: (0, s.default)(L.start_day).isAfter(
                      (0, s.default)(),
                    ),
                    isSubscribe: L.subscribe,
                    info_duration:
                      null !== (v = n.data.info) &&
                      void 0 !== v &&
                      null !== (w = v.audio) &&
                      void 0 !== w &&
                      null !== (m = w[0]) &&
                      void 0 !== m &&
                      m.time
                        ? (0, i.format)(n.data.info.audio[0].time)
                        : "00:00",
                    currentAudio:
                      null != L &&
                      null !== (x = L.audio) &&
                      void 0 !== x &&
                      null !== (b = x[0]) &&
                      void 0 !== b &&
                      b.url
                        ? -1
                        : 0,
                    newList: N,
                  }),
                  (0, c.report)("exhibit_show", { exhibitname: L.name });
              case 25:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  onCloseLoginPop: function () {
    this.setData({ isShowLoginPop: !1 });
  },
  onLogin: function () {
    var t = this;
    return a(
      e.default.mark(function a() {
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                t.setData({ isShowLoginPop: !1 });
              case 1:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  onChangeItem: function (t) {
    var e = t.detail.current;
    this.setData({ current: e });
  },
  onBack: function () {
    getCurrentPages().length > 1
      ? wx.navigateBack({ delta: 1 })
      : wx.reLaunch({ url: r.default.PAGE_INDEX }),
      wx.getBackgroundAudioManager().stop();
  },
  onSwiperImg: function (t) {
    var e = t.currentTarget.dataset.current;
    wx.previewImage({ current: e, urls: this.data.info.album, showmenu: !1 });
  },
  scroll: function (t) {
    var e = t.detail.scrollTop;
    this.setData({
      background:
        e < 60
          ? "linear-gradient(0deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.5) 100%)"
          : "#FAF3E8",
      iconTheme: e > 60 ? "#000" : "white",
      titleColor: e > 60 ? "#000" : "white",
    }),
      e > 60
        ? wx.setNavigationBarColor({
            backgroundColor: "#FFf",
            frontColor: "#000000",
          })
        : wx.setNavigationBarColor({
            backgroundColor: "#000000",
            frontColor: "#ffffff",
          });
  },
  showMore: function () {
    this.setData({ more: !this.data.more });
  },
  handleAudioManageFunc: function (t) {
    (t.currentTarget.dataset.index = 0),
      this.data.paused ? this.onPlay(t) : this.onStop();
    var e = this.data.paused;
    this.setData({ paused: !e });
  },
  handleSliderChange: function (t) {
    this.setData({ currentAudio: -1 });
    var e = t.detail.value;
    this.setData({ progress: e, current_time: (0, i.format)(e) }),
      wx.getBackgroundAudioManager().seek(e);
  },
  toPlay: function (t) {
    var e;
    this.setData({ paused: !0, progress: 0 });
    var a = this.data.info;
    null != a &&
      null !== (e = a.audio) &&
      void 0 !== e &&
      e[0] &&
      (t.currentTarget.dataset.index += 1),
      this.onPlay(t);
  },
  onStop: function () {
    wx.getBackgroundAudioManager().pause();
  },
  onPlay: function (t) {
    var e,
      a,
      n = t.currentTarget.dataset.item;
    if (
      (null == n ||
      null === (e = n.audio) ||
      void 0 === e ||
      null === (a = e[0]) ||
      void 0 === a
        ? void 0
        : a.url) !== wx.getBackgroundAudioManager().src
    ) {
      (wx.getBackgroundAudioManager().title = n.name),
        (wx.getBackgroundAudioManager().src = n.audio[0].url),
        this.setData({
          audio_duration: n.audio[0].time || 0,
          audio_duration_val: (0, i.format)(n.audio[0].time) || "00:00",
          audio_status: 1,
        });
      var o = this.data.newList.findIndex(function (t) {
        return t.audio[0] && t.audio[0].url === n.audio[0].url;
      });
      this.audioInit(o);
    }
    wx.getBackgroundAudioManager().play();
  },
  audioInit: function (t) {
    var e = this,
      a = this.data.list.findIndex(function (a) {
        var i;
        return (
          (null === (i = a.audio[0]) || void 0 === i ? void 0 : i.url) ===
          e.data.newList[t].audio[0].url
        );
      });
    wx.getBackgroundAudioManager().onPlay(function () {
      e.setData({
        footerAudio: t,
        currentAudio: a,
        audio_status: 1,
        duration: "0%",
        isShow: !0,
        audio_duration_val: (0, i.format)(e.data.newList[t].audio[0].time),
      });
    }),
      wx.getBackgroundAudioManager().onStop(function () {
        e.setData({ audio_status: 0, current_time: "00:00", duration: "100%" });
      }),
      wx.getBackgroundAudioManager().onEnded(function () {
        e.setData({
          paused: !0,
          audio_status: 0,
          current_time: "00:00",
          duration: "100%",
          progress: 0,
        });
      }),
      wx.getBackgroundAudioManager().onPause(function () {
        e.setData({ audio_status: 2 });
      }),
      wx.getBackgroundAudioManager().onTimeUpdate(function () {
        var a = e.data,
          n = a.currentAudio,
          o = a.audio_status,
          r = wx.getBackgroundAudioManager().currentTime,
          u = e.data.newList[t].audio[0].time - r;
        e.setData({
          audio_current: (0, i.format)(u),
          current_time: (0, i.format)(r),
          duration: "".concat((r / u) * 100, "%"),
        }),
          -1 === n && e.setData({ progress: r }),
          0 === o && e.setData({ duration: "100%" });
      }),
      wx.getBackgroundAudioManager().onSeeked(function () {
        e.setData({ duration: "100%" });
      });
  },
  toNext: function (t) {
    var e = this,
      a = this.data,
      n = a.newList,
      o = a.info,
      r = a.footerAudio;
    if (!(r >= n.length - 1)) {
      var u = t.currentTarget.dataset.item,
        s = this.data.list.findIndex(function (t) {
          return t.audio[0] && t.audio[0].url === u.audio[0].url;
        });
      wx.getBackgroundAudioManager().stop(),
        setTimeout(function () {
          var a;
          e.setData({
            currentAudio: s,
            current_time: (0, i.format)(0),
            audio_duration_val:
              (0, i.format)(n[r].audio[0].time) || (0, i.format)(0),
            duration: "0%",
            audio_status: 1,
          }),
            -1 !== s &&
              null != o &&
              null !== (a = o.audio) &&
              void 0 !== a &&
              a[0] &&
              e.setData({ paused: !0, progress: 0 }),
            e.onPlay(t);
        }, 100);
    }
  },
  toPrev: function (t) {
    var e = this,
      a = this.data,
      n = a.newList,
      o = a.info,
      r = a.footerAudio;
    if (!(r <= 0)) {
      var u = t.currentTarget.dataset.item,
        s = this.data.list.findIndex(function (t) {
          return t.audio[0] && t.audio[0].url === u.audio[0].url;
        });
      wx.getBackgroundAudioManager().stop(),
        setTimeout(function () {
          e.setData({
            currentAudio: s,
            current_time: (0, i.format)(0),
            audio_duration_val:
              (0, i.format)(n[r].audio[0].time) || (0, i.format)(0),
            duration: "0%",
            audio_status: 1,
          }),
            s < 0 && o.audio[0] && e.setData({ paused: !1 }),
            e.onPlay(t);
        }, 100);
    }
  },
  change: function (t) {
    var e = this.data,
      a = e.audio_status,
      i = e.paused,
      n = e.info;
    0 === e.footerAudio && n.audio[0] && this.setData({ paused: !i }),
      1 === a ? this.onStop() : this.onPlay(t);
  },
  onShareAppMessage: function () {
    return {
      title: n.default.share.title,
      imageUrl: n.default.share.pic,
      path: ""
        .concat(r.default.PAGE_RECOMMEND_EXHIBITION_CARD, "?id=")
        .concat(this.data.exhibition_id),
    };
  },
  exhibitionCollect: function () {
    var t = this;
    return a(
      e.default.mark(function a() {
        var i, n, r, u, s, c;
        return e.default.wrap(function (e) {
          for (;;)
            switch ((e.prev = e.next)) {
              case 0:
                if ((0, d.getLoginStatus)()) {
                  e.next = 4;
                  break;
                }
                return t.setData({ isShowLoginPop: !0 }), e.abrupt("return");
              case 4:
                return (
                  (i = t.data),
                  (n = i.type),
                  (r = i.openId),
                  (u = i.isCollection),
                  (s = i.exhibition_id),
                  (e.next = 7),
                  o.saveOrCancelCollection({
                    category: n,
                    id: s,
                    openId: r,
                    operate: u ? 0 : 1,
                  })
                );
              case 7:
                (c = e.sent),
                  t.setData({ isCollection: 0 === c.code ? !u : u }),
                  wx.showToast({
                    title: c.msg ? c.msg : "已".concat(u ? "取消" : "", "收藏"),
                    icon: "none",
                    duration: 1e3,
                  });
              case 10:
              case "end":
                return e.stop();
            }
        }, a);
      }),
    )();
  },
  goToDestination: function () {
    var t = this.data,
      e = t.longitude,
      a = t.latitude,
      i = t.info;
    wx.getBackgroundAudioManager().stop(),
      (0, u.navigateTo)(r.default.PAGE_LBS_TRANSFER, {
        destPoint: JSON.stringify({
          latitude: i.location.lat,
          longitude: i.location.lon,
          name: i.name,
        }),
        startPoint: JSON.stringify({
          latitude: a,
          longitude: e,
          name: "我的位置",
        }),
      });
  },
  toInfo: function (t) {
    wx.getBackgroundAudioManager().stop();
    var e = t.currentTarget.dataset.item;
    (0, u.navigateTo)(r.default.PAGE_APPRECIATION_PAGES_CULTURAL_INDEX, {
      id: e.exhibit_id,
    });
  },
  subscribeMessage: function () {
    var t = this;
    return a(
      e.default.mark(function i() {
        return e.default.wrap(function (i) {
          for (;;)
            switch ((i.prev = i.next)) {
              case 0:
                if (!t.data.isSubscribe) {
                  i.next = 4;
                  break;
                }
                return (
                  wx.showToast({
                    title: "您已订阅该消息!",
                    mask: !0,
                    icon: "none",
                  }),
                  i.abrupt("return")
                );
              case 4:
                wx.getSetting({
                  withSubscriptions: !0,
                  success: function (i) {
                    var n;
                    console.log("setting", i),
                      wx.requestSubscribeMessage({
                        tmplIds: [
                          "5Vt07w-znLRhbMTx190QHSs6eQb2yCddc_0jlzfNfFE",
                        ],
                        success:
                          ((n = a(
                            e.default.mark(function a(i) {
                              var n;
                              return e.default.wrap(function (e) {
                                for (;;)
                                  switch ((e.prev = e.next)) {
                                    case 0:
                                      if (
                                        (console.log("sub", i),
                                        (n =
                                          i[
                                            "5Vt07w-znLRhbMTx190QHSs6eQb2yCddc_0jlzfNfFE"
                                          ]),
                                        console.log(n),
                                        "accept" === n)
                                      ) {
                                        e.next = 6;
                                        break;
                                      }
                                      return (
                                        wx.showToast({
                                          title: "订阅消息被拒绝",
                                          mask: !0,
                                          icon: "none",
                                        }),
                                        e.abrupt("return")
                                      );
                                    case 6:
                                      return (
                                        (e.next = 8),
                                        o.subscribeExhibition({
                                          open_id: t.data.openId,
                                          exhibition_id: t.data.exhibition_id,
                                        })
                                      );
                                    case 8:
                                      t.setData({ isSubscribe: !0 }),
                                        wx.showToast({
                                          title: "消息订阅成功!",
                                          mask: !0,
                                          icon: "none",
                                        });
                                    case 10:
                                    case "end":
                                      return e.stop();
                                  }
                              }, a);
                            }),
                          )),
                          function (t) {
                            return n.apply(this, arguments);
                          }),
                        fail: function (t) {
                          console.log("err", t);
                        },
                      });
                  },
                });
              case 5:
              case "end":
                return i.stop();
            }
        }, i);
      }),
    )();
  },
});
